package com.ssi.myshop.base;

import android.util.Log;

import io.reactivex.observers.ResourceObserver;

public abstract class BaseObserver<T> extends ResourceObserver<BaseData<T>> {
    private static final String TAG = "BaseObserver";
    private IBaseCallBack mCallBack;

    public BaseObserver(IBaseCallBack callBack) {
        this.mCallBack = callBack;
    }

    @Override
    protected void onStart() {
        mCallBack.onLoading();
        super.onStart();
    }

    @Override
    public void onNext(BaseData<T> tBaseData) {
        Log.d(TAG, "onNext: " + tBaseData.getData());
        if (tBaseData.getCode() == 0) {
            onSuccess(tBaseData.getData());
            mCallBack.onSuccess();
        } else {
            mCallBack.onError(tBaseData.getMsg());
        }
    }

    @Override
    public void onError(Throwable e) {
        onFailed(e);
        mCallBack.onError(e.toString());
        Log.d(TAG, "onError: " + e.getMessage());
    }

    @Override
    public void onComplete() {
    }

    private void onFailed(Throwable e) {
    }

    protected abstract void onSuccess(T data);

}
